---
layout: api
title: "v2.0.0 JavaScript Library: Events methods"
categories: api
version: v2.0.0
permalink: /api/v2.0.0/l-events/
---
<h2 id="events">Events methods</h2>

<p>A set of methods shared between event-powered classes (like Map). Generally, events allow you to execute some function when something happens with an object (e.g. the user clicks on the map, causing the map <code><span class="string">'fire'</span></code> event).</p>

<h3>Example</h3>

<pre><code class="javascript">map.on('click', function(e) {
	alert(e.latlng);
});</code></pre>

<p>Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:</p>

<pre><code>function onClick(e) { ... }

map.on('click', onClick);
map.off('click', onClick);</code></pre>

<h3>Methods</h3>
<table data-id='events'>
	<tr>
<th>Method</th>
<th>Returns</th>
<th>Description</th>
	</tr>
	<tr>
<td><code><b>addEventListener</b>(
<nobr>&lt;String&gt; <i>type</i></nobr>,
<nobr>&lt;Function&gt; <i>fn</i></nobr>,
<nobr>&lt;Object&gt; <i>context?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Adds a listener function (<code>fn</code>) to a particular event type of the object. You can optionally specify the context of the listener (object the <code><span class="keyword">this</span></code> keyword will point to). You can also pass several space-separated types (e.g. <code><span class="string">'click dblclick'</span></code>).</td>
	</tr>
	<tr>
<td><code><b>addOneTimeEventListener</b>(
<nobr>&lt;String&gt; <i>type</i></nobr>,
<nobr>&lt;Function&gt; <i>fn</i></nobr>,
<nobr>&lt;Object&gt; <i>context?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>The same as above except the listener will only get fired once and then removed.</td>
	</tr>
	<tr>
<td><code><b>addEventListener</b>(
<nobr>&lt;Object&gt; <i>eventMap</i></nobr>,
<nobr>&lt;Object&gt; <i>context?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Adds a set of type/listener pairs, e.g. <code>{click: onClick, mousemove: onMouseMove}</code></td>
	</tr>
	<tr>
<td><code><b>removeEventListener</b>(
<nobr>&lt;String&gt; <i>type</i></nobr>,
<nobr>&lt;Function&gt; <i>fn?</i></nobr>,
<nobr>&lt;Object&gt; <i>context?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object.  Note that if you passed a custom context to <code>addEventListener</code>, you must pass the same context to <code>removeEventListener</code> in order to remove the listener.</td>
	</tr>
	<tr>
<td><code><b>removeEventListener</b>(
<nobr>&lt;Object&gt; <i>eventMap</i></nobr>,
<nobr>&lt;Object&gt; <i>context?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Removes a set of type/listener pairs.</code></td>
	</tr>
	<tr>
<td><code><b>removeEventListener</b>()</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Removes all listeners. An alias to <code>clearAllEventListeners</code> when you use it without arguments.</td>
	</tr>
	<tr>
<td><code><b>hasEventListeners</b>(
<nobr>&lt;String&gt; <i>type</i> )</nobr>
</code></td>

<td><code>Boolean</code></td>
<td>Returns <code><span class="literal">true</span></code> if a particular event type has some listeners attached to it.</td>
	</tr>
	<tr>
<td><code><b>fireEvent</b>(
<nobr>&lt;String&gt; <i>type</i></nobr>,
<nobr>&lt;Object&gt; <i>data?</i> )</nobr>
</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Fires an event of the specified type. You can optionally provide an data object &mdash; the first argument of the listener function will contain its properties.</td>
	</tr>
	<tr>
<td><code><b>clearAllEventListeners</b>()</code></td>

<td><code><span class="keyword">this</span></code></td>
<td>Removes all listeners to all events on the object.</code></td>
	</tr>
	<tr>
<td><code><b>on</b>( &hellip; )</code></td>
<td><code><span class="keyword">this</span></code></td>
<td>Alias to <code>addEventListener</code>.</td>
	</tr>
	<tr>
<td><code><b>once</b>( &hellip; )</code></td>
<td><code><span class="keyword">this</span></code></td>
<td>Alias to <code>addOneTimeEventListener</code>.</td>
	</tr>
	<tr>
<td><code><b>off</b>( &hellip; )</code></td>
<td><code><span class="keyword">this</span></code></td>
<td>Alias to <code>removeEventListener</code>.</td>
	</tr>
	<tr>
<td><code><b>fire</b>( &hellip; )</code></td>
<td><code><span class="keyword">this</span></code></td>
<td>Alias to <code>fireEvent</code>.</td>
	</tr>
</table>

